package doublestack

import "fmt"

/*
DoubleStack 结构体
*/
type DoubleStack struct {
	top1 int64
	top2 int64
	size int64
	data []interface{}
}

//InitStack 栈初始化
func InitStack(size int64) DoubleStack {
	d := DoubleStack{}
	d.top1 = -1
	d.top2 = size
	d.size = size
	d.data = make([]interface{}, size)
	return d
}

/*
Push 向栈中插入元素
data 插入的元素
stackNum 栈编号
*/
func (d *DoubleStack) Push(data interface{}, stackNum int64) {
	if d.IsFull() {
		fmt.Println("栈空间已满, 无法继续插入")
		return
	}
	if stackNum == 1 {
		// 向栈1 中插入元素
		d.top1++
		d.data[d.top1] = data
	} else {
		d.top2--
		d.data[d.top2] = data
	}
}

//IsFull 判断栈是否已满
func (d *DoubleStack) IsFull() bool {
	return d.top1+1 == d.top2
}

/*
Pop 弹出元素
stackNum 栈编号
*/
func (d *DoubleStack) Pop(stackNum int64) (data interface{}) {
	if stackNum == 1 {
		if d.top1 == -1 {
			fmt.Printf("栈%d 已空, 无法继续弹出", stackNum)
			return
		}
		data = d.data[d.top1]
		d.top1--
	} else {
		if d.top2 >= d.size {
			fmt.Printf("栈%d已空,无法继续弹出", stackNum)
		}
		data = d.data[d.top2]
		d.top2++
	}
	return data
}

//IsEmpty 判断栈空间是否已经彻底的空了
func (d *DoubleStack) IsEmpty() bool {
	if d.top1 == -1 && d.top2 == d.size {
		return true
	}
	return false
}

/*
Show 根据栈编号遍历栈
*/
func (d *DoubleStack) Show(stackNum int64) {
	var i int64
	if stackNum == 1 {
		for i = 0; i <= d.top1; i++ {
			fmt.Println(d.data[i])
		}
	} else {
		for i = d.size - 1; i >= d.top2; i-- {
			fmt.Println(d.data[i])
		}
	}
}

/*
Clear 清空栈
*/
func (d *DoubleStack) Clear() {
	d.top1 = -1
	d.top2 = d.size
}
